import { message } from 'antd';
import router from 'umi/router';
import {
  AccusearrchList,
  AccusearrchDicList,
  startHandle,
  originCircle,
  accusearrchDetail,
} from '@/services/socialmanage';

export default {
  state: {
    formFieldsValue: {},
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    // tabState:'1',
    paymentmethod: [], // 缴费方式
    selectedRows: [],
    selectedKeys: [],
    nameContent: '',
    nameTitle: '',
    startList: {
      empSendInfoVo: {},
      empBaseInfoVo: {},
      insSerHdl: {},
      matlist: [],
      originalRemark: {},
      selectedRows: [],
      remmarkList: [],
      insRefundInfo: {
        list: [],
        pagination: {
          current: 1,
          pageSize: 10,
          total: 10,
        },
      },
    },
    detail: {
      empInfo: {},
      list: [],
    },
    expandForm: false,
    monthvalue: [],
  },

  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put, select }) {
      const pathname = yield select(state => state.routing.location.pathname);
      let params = payload;
      if (pathname === '/socaccmanagesend/accusearrchsend/index') {
        params = { ...params, paramType: 2 };
      } else if (pathname === '/socaccmanagereceive/accusearrchreceive/index') {
        params = { ...params, paramType: 1 };
      }
      const response = yield call(AccusearrchList, params);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取列表数据失败：服务端未知错误');
        yield put({
          type: 'getList',
          payload: {
            data: {},
            currentPage,
            pageSize,
          },
        });
      }
    },
    // 字典查询
    *fetchDicList({ payload }, { call, put }) {
      const response = yield call(AccusearrchDicList, payload);
      if (response.success) {
        yield put({
          type: 'dicList',
          payload: {
            data: response.data,
          },
        });
      }
    },
    // *fetchStartList({ payload }, { call, put }) {
    //   const response = yield call(accusearrchStartList, payload);
    //   if (response.success) {
    //     yield put({
    //       type: 'startList',
    //       payload: {
    //         data: response.data,
    //       },
    //     });
    //   }
    // },

    // *accusearrchDetail({ payload }, { call, put }) {
    //   const response = yield call(AccusearrchDetail, payload);
    //   if (response.success) {
    //     yield put({
    //       type: 'startList',
    //       payload: {
    //         data: response.data,
    //       },
    //     });
    //   }
    // },

    // 原件流转
    *circle({ payload, callback }, { call }) {
      const response = yield call(originCircle, payload);
      if (callback) callback(response);
    },

    // 开始办理
    *startHandle({ payload }, { call }) {
      const response = yield call(startHandle, payload);
      if (response.success) {
        router.goBack();
      }
    },

    // 获取详情
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(accusearrchDetail, payload);
      if (response.success) {
        yield put({
          type: 'getDetail',
          payload: response.data,
        });
      }
    },
  },

  reducers: {
    // 获取列表

    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      // const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      // return {
      //   ...state,
      //   data: {
      //     list,
      //     pagination: { total: data.totalCount || 0, current: currentPage, pageSize: pageSizes },
      //   },
      // };
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      const pageArr = [];
      if (list.length) {
        list.forEach(item => {
          if (item.empAssList.length) {
            // 基数 empBase+ unitBase
            // 个人比例  empProp
            // 个人缴费  empAmo
            // 公司比例  unitProp
            // 公司缴费  unitAmo
            const obj = {};
            item.empAssList.forEach(c => {
              obj[`empBase_${c.ass}`] = c.empBase || +c.empBase === 0 ? c.empBase : '--';
              obj[`unitBase_${c.ass}`] = c.unitBase || +c.unitBase === 0 ? c.unitBase : '--';
              obj[`empProp_${c.ass}`] = c.empProp || +c.empProp === 0 ? c.empProp : '--';
              obj[`empAmo_${c.ass}`] = c.empAmo || +c.empAmo === 0 ? c.empAmo : '--';
              obj[`unitProp_${c.ass}`] = c.unitProp || +c.unitProp === 0 ? c.unitProp : '--';
              obj[`unitAmo_${c.ass}`] = c.unitAmo || +c.unitAmo === 0 ? c.unitAmo : '--';
              obj[`empIndvAccoNo_${c.ass}`] =
                c.empIndvAccoNo || +c.empIndvAccoNo === 0 ? c.empIndvAccoNo : '--';
            });
            pageArr.push({ ...item, ...obj });
          }
        });
      }
      return {
        ...state,
        data: {
          list: pageArr,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    dicList(
      state,
      {
        payload: { data },
      }
    ) {
      return {
        ...state,
        paymentmethod: data.PAY_FEES_WAY,
      };
    },
    selectRow(state, { payload }) {
      return {
        ...state,
        selectedRows: payload.selectedRows,
        selectedKeys: payload.selectedKeys,
      };
    },
    toggleVisible(state, { payload }) {
      return {
        ...state,
        circleVisible: !payload,
      };
    },
    originCircle(state, { payload }) {
      return {
        ...state,
        circleVisible: !payload,
      };
    },

    getDetail(state, { payload }) {
      return {
        ...state,
        detail: payload,
      };
    },

    startList(state, { payload }) {
      const {
        empSendInfoVo,
        empBaseInfoVo,
        insSerHdl,
        matlist,
        origInalRemark,
        insRefundInfo,
        selectedRows,
        remmarkList,
      } = payload.data;
      return {
        ...state,
        startList: {
          empSendInfoVo,
          empBaseInfoVo,
          insSerHdl,
          matlist,
          origInalRemark,
          insRefundInfo,
          selectedRows,
          remmarkList,
        },
      };
    },

    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
